#coding=utf-8
from bottle import request
from config import config

def seterror(text=""):
	if text is not None:
		if 'errors' not in request.g:
			request.g['errors'] = []
		request.g['errors'].append("error : " + str(text) + "\n")

def geterror():
	return request.g['errors']

def setlog(text=""):
	if 'logs' not in request.g:
		request.g['logs'] = []
	request.g['logs'].append(str(text) + "\n")

def getlog():
	return request.g['logs']

def settime(text=""):
	if 'handler_time' not in request.g:
		request.g['handler_time'] = []
	request.g['handler_time'].append(str(text) + "\n")

def save():
	import time
	#error_log
	if 'errors' in request.g:
		logfil = open(config.LOG_PATH + '/error.' \
			+ time.strftime('%Y-%m-%d', time.localtime()),'a')
		logfil.writelines(''.join(request.g['handler_time']))
		logfil.writelines(''.join(request.g['errors']))
		logfil.close()

	#info_log
	if 'handler_time' in request.g:
		logfil = open(config.LOG_PATH + '/logs.' \
			+ time.strftime('%Y-%m-%d', time.localtime()),'a')
		logfil.writelines(''.join(request.g['handler_time']))
		if 'logs' in request.g:
			logfil.writelines(''.join(request.g['logs']))
		logfil.close()
	request.g['errors'] = []
	request.g['logs'] = []